package com.erp.module.JiJianCaiJi

import android.app.Activity
import android.content.Intent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.CaptureActivity
import com.erp.R
import com.erp.bean.JiJianCaiJiBean
import com.erp.myAdapter.JiJianCaiJiAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.permission.PermissionListener
import com.erp.util.Myutil
import com.erp.util.PermissionUtils
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_jijian_caiji.*

/**
 * Created by Administrator on 2018/3/30/030.
 * 作者：刘洋
 * 计件采集页面
 */
class FragmentKJiJianCaiJi : BaseKFragment(), JiJianCaiJiContract.JiJianCaiJiView, View.OnClickListener, AdapterView.OnItemLongClickListener, PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener {

    lateinit var jiJianCaiJiBeans : MutableList<JiJianCaiJiBean>
    lateinit var adapter : JiJianCaiJiAdapter
    lateinit var P: IJiJianCaiJiP
    var updialog: AlertDialog?=null
    private val f = "FragmentKJiJianCaiJi"
    override fun getLayoutId(): Int {
        fragmentname=f
        return R.layout.fragment_jijian_caiji
    }

    override fun initData() {
        jiJianCaiJiBeans=ArrayList()
        adapter= JiJianCaiJiAdapter(jiJianCaiJiBeans, activity!!)
        JiJianLv.adapter=adapter
        JiJianLv.onItemLongClickListener=this
        P= IJiJianCaiJiP(this, context!!,jiJianCaiJiBeans)
        JiJianFragmentPv.setheader(true)
        JiJianFragmentPv.setOnHeaderRefreshListener(this)
        JiJianFragmentPv.setOnFooterRefreshListener(this)
//        shuRuEt.setOnEditorActionListener { v, actionId, event ->
//            var isOK = true
//            when (actionId) {
//                EditorInfo.IME_ACTION_SEARCH -> {
//                    updialog = Myutil.showupdatedialog(activity, "正在刷新", false)
//                    P.sousuo(EtSouSuo.text.toString())
//                }
//                else -> isOK = false
//            }
//            isOK
//        }
        EtSouSuo.setOnEditorActionListener { v, actionId, event ->
            var isOK = true
            when (actionId) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    updialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                    P.sousuo(EtSouSuo.text.toString())
                }
                else -> isOK = false
            }
            isOK
        }
        shuangChuanBt.setOnClickListener(this)
        saoMiaoIv.setOnClickListener(this)
        BtGuoLv.setOnClickListener(this)
        P.gainData("JiJianCaiJi")
        JiJianFragmentPv.headerRefreshing()
    }
    fun  qingkong(){
        if(isFirstLoad){
            return
        }
        JiJianFragmentPv.headerRefreshing()
    }
    override fun onClick(v: View?) {
        when(v){
            BtGuoLv -> {
                updialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                P.sousuo(EtSouSuo.text.toString())
            }
            shuangChuanBt -> {
                var mcode=shuRuEt.text.toString().trim()
                if(mcode==""){
                       Myutil.MyTost("请先输入条码或者扫描后上传")
                }else{
                    updialog = Myutil.showupdatedialog(activity, "正在上传", false)
                    P.putJiJianShuJu(mcode)
                }
            }
            saoMiaoIv -> {
                PermissionUtils.issaomiao(activity!!,object : PermissionListener {
                    override fun onSucceed(requestCode: Int, grantPermissions: MutableList<String>) {
                        val intent=Intent(activity, CaptureActivity:: class.java)
                        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                        startActivityForResult(intent, 2)
                    }

                    override fun onFailed(requestCode: Int, deniedPermissions: MutableList<String>) {
                        Myutil.MyTost("没有权限，无法扫描")
                    }

                })
            }
        }
    }
    /**获取扫描数据*/
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null||resultCode!= Activity.RESULT_OK){
            return
        }
        when (requestCode) {
            2 -> {
                val bundle = data.extras
                val s = bundle?.get("result") as String
                shuRuEt.setText(s)
            }
        }
    }

    override fun setJiJianCaiJiOk() {
        updialog?.dismiss()
        P.xiala()
        updialog = Myutil.showupdatedialog(activity, "正在刷新", false)
        shuRuEt.setText("");
    }

    override fun setJiJianCaiJiNo(throwable : String) {
        updialog?.dismiss()
        Myutil.MyTost(throwable)
    }
    override fun deleteJiJianCaiJiOk(item : Int) {
        Myutil.MyTost("删除成功")
        jiJianCaiJiBeans.removeAt(item)
        updialog?.dismiss()
        adapter.notifyDataSetChanged()
    }

    override fun deleteJiJianCaiJiNo(throwable: String) {
        Myutil.MyTost("throwable")
        updialog?.dismiss()
    }
    override fun onItemLongClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long): Boolean {
        var c = context;
        if(c != null){
            val dialog = AlertDialog.Builder(c)
            dialog.setTitle("提示")
            dialog.setMessage("确定要删除  " + jiJianCaiJiBeans.get(position).mb_batch + " 这条计件采集吗?")
            dialog.setPositiveButton("确定") { dialog, which ->
                updialog = Myutil.showupdatedialog(activity, "正在删除", false)
                dialog.dismiss()
                P.deleteJiJianShuJu(jiJianCaiJiBeans[position].id,position)
            }
            dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
            dialog.show()
            return true
        }
        return true
    }

    override fun xialaOk() {
        updialog?.dismiss()
        JiJianFragmentPv?.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updialog?.dismiss()
        JiJianFragmentPv?.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updialog?.dismiss()
        JiJianFragmentPv?.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updialog?.dismiss()
        JiJianFragmentPv?.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }
}